package com.example.jdbcsamples.program;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * @author Bora SAYINER
 * @since Feb 1, 2015 10:33:23 AM
 * @version 1.0.0
 */
public class Driver {

	private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
	private static final String DB_USER_NAME = "ex";
	private static final String DB_PASSWORD = "ex";

	public static void main(String[] args) {
		try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER_NAME, DB_PASSWORD)) {
			try (Statement statement = connection.createStatement()) {
				statement.setMaxRows(3);
				try (ResultSet resultSet = statement.executeQuery("SELECT * FROM employees")) {
					while (resultSet.next()) {
						System.out.println(resultSet.getInt("employee_id") + " " + resultSet.getString("first_name")
								+ " " + resultSet.getString("last_name"));
					}
				}
			}
		} catch (SQLException e) {
			printSqlError(e);
		}
		
	}

	public static void printSqlError(SQLException exception) {
		System.err.println("Error message: " + exception.getMessage());
		System.err.println("Error code: " + exception.getErrorCode());
		System.err.println("Sql state: " + exception.getSQLState());
	}
}
